<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="项目性能和规范总结"><meta name="keywords" content="性能"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>项目性能和规范总结 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#使用-eslint-格式化指定文件"><span class="toc-number">1.</span> <span class="toc-text">使用 eslint 格式化指定文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#重写格式规则"><span class="toc-number">2.</span> <span class="toc-text">重写格式规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#提交的时候自动格式化"><span class="toc-number">3.</span> <span class="toc-text">提交的时候自动格式化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用-prettier-进行格式化"><span class="toc-number">4.</span> <span class="toc-text">使用 prettier 进行格式化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#分析各个模块的代码体积"><span class="toc-number">5.</span> <span class="toc-text">分析各个模块的代码体积</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#禁止打包的时候自动启动服务"><span class="toc-number">6.</span> <span class="toc-text">禁止打包的时候自动启动服务</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">项目性能和规范总结</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-01-09</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/%E6%80%A7%E8%83%BD/">性能</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h3 id="使用-eslint-格式化指定文件"><a href="#使用-eslint-格式化指定文件" class="headerlink" title="使用 eslint 格式化指定文件"></a>使用 eslint 格式化指定文件</h3><p>在<code>package.json</code>文件中增加通过 eslint 格式代码的方式。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">"scripts": &#123;</span><br><span class="line">  "start": "node scripts/start.js",</span><br><span class="line">  "build": "node scripts/build.js",</span><br><span class="line">  "test": "node scripts/test.js",</span><br><span class="line">  "format": "eslint src/**/*.&#123;jsx,js&#125; --fix", // 这行的配置</span><br><span class="line">  "deploy": "gh-pages -d build"</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<p>执行<code>npm run format</code>就会使用<code>eslint</code>的规则对<code>src</code>路径下的<code>jsx</code>和<code>js</code>文件进行格式化。<br>使用的规则可以在<code>eslintConfig</code>字段就行配置，比如默认使用的是 react-app 的规则。这个规则非常有限。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">"eslintConfig": &#123;</span><br><span class="line">  "extends": "react-app",</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以使用 eslint 推荐的规则进行格式化。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">"eslintConfig": &#123;</span><br><span class="line">  "extends": [</span><br><span class="line">        “eslint:recommended”,</span><br><span class="line">        <span class="string">"react-app"</span></span><br><span class="line">  ],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="重写格式规则"><a href="#重写格式规则" class="headerlink" title="重写格式规则"></a>重写格式规则</h3><p>在 eslintConfig 中的 rule 字段进行设置。比如下面是要求 jsx 的缩进是 4 个空格的空白。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"> &quot;eslintConfig&quot;: &#123;</span><br><span class="line">  &quot;extends&quot;: [</span><br><span class="line">        “eslint:recommended”,</span><br><span class="line">        &quot;react-app&quot;</span><br><span class="line">  ],</span><br><span class="line"> &quot;rules&quot;:&#123;</span><br><span class="line">        &quot;react&#x2F;jsx-indent&quot; :[“error”,4]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行<code>npm run format</code>后，不符合规则的文件将被修复成符合规则的格式。</p>
<h3 id="提交的时候自动格式化"><a href="#提交的时候自动格式化" class="headerlink" title="提交的时候自动格式化"></a>提交的时候自动格式化</h3><p>提交的时候做校验，需要用到 git 的 hooks，由于自己配置比较麻烦，可以使用第三方的公交<code>husky</code></p>
<p>另外需要配合 lint-staged，lint-staged 是被 husky 进行调用，然后进行规则验证。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i husky lint-staged -D</span><br></pre></td></tr></table></figure>

<p>安装完成后后 package.json 中进行配置。增加根字段 husky。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">"husky":&#123;</span><br><span class="line">    "hooks":&#123;</span><br><span class="line">          "pre-commit":"lint-staged"</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br><span class="line">"lint-staged":&#123;</span><br><span class="line">    "*.&#123;js,jsx&#125;":[</span><br><span class="line">            "eslint --fix",</span><br><span class="line">             <span class="string">"git add "</span></span><br><span class="line">      ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面是对 pre-commit 进行了设置，其实 hooks 有很多种。<br>上面的配置意思是：提交之前执行 lint-staged 命令（在 modules 的 bin 文件夹中可以找到）， 然后配置 lint-staged 命令的规则。<br>当执行<code>git commit -am &quot;test&quot;</code>时，就会自动执行 lint-staged 的规则。</p>
<h3 id="使用-prettier-进行格式化"><a href="#使用-prettier-进行格式化" class="headerlink" title="使用 prettier 进行格式化"></a>使用 prettier 进行格式化</h3><p>由于 eslint 的格式化功能比较弱，并且支持的文件格式也不多，所以使用更强大的 prettier 会更合适，prettier 不仅能格式化 js、jsx 还能格式化 css 和 md 文件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i prettier -D</span><br></pre></td></tr></table></figure>

<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">"lint-staged":&#123;</span><br><span class="line">    "*.&#123;js,jsx&#125;":[</span><br><span class="line">            "prittier --write",</span><br><span class="line">            "eslint --fix",</span><br><span class="line">             <span class="string">"git add "</span></span><br><span class="line">      ],</span><br><span class="line">    "*.&#123;css,md&#125;":[</span><br><span class="line">          "prittier --write",</span><br><span class="line">          <span class="string">"git add"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个改动会比较大，所以开发的时候配置的规则最好就是 prettier，不然提交后代码变动较大。</p>
<p>可以在根目录创建 prettier 的配置文件 prettier.config.js，来自定义规则</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  tabWidth: <span class="number">4</span>,</span><br><span class="line">  singleQuote: <span class="literal">true</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>使用了 husky 后，手动执行命令的方式就不要了。</p>
</blockquote>
<h3 id="分析各个模块的代码体积"><a href="#分析各个模块的代码体积" class="headerlink" title="分析各个模块的代码体积"></a>分析各个模块的代码体积</h3><p>使用<code>webpack-bundle-analyzer</code>可以分析各个模块的体积大小</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i webpack-bundle-analyzer -D</span><br></pre></td></tr></table></figure>

<p>在 webpack.config.js 配置</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> BundleAnalyzerPlugin = <span class="built_in">require</span>(<span class="string">"webpack-bundle-analyzer"</span>).BundleAnalyzerPlugin;</span><br></pre></td></tr></table></figure>

<p>在 plugins 中增加</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">plugins:[</span><br><span class="line">      <span class="keyword">new</span> BundleAnalyzerPlugin(),</span><br><span class="line">      <span class="keyword">new</span> HtmlWebpackPlugin(....</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<p>配置好后，重新启动程序</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm start</span><br></pre></td></tr></table></figure>

<p>这次启动除了原本服务之外，还会启动并打开一个<code>http://127.0.0.1:8888/</code>的服务，打开这个就能看到各个模块的体积了。<br>这种方式看到是开发模式下的体积，意义不大，看到打包后放到生产环境上的体积才是有意义的。<br>安装了这个插件后，执行打包后也会自动启动 8888 服务，这时候看到的就是生产环境上的体积了(注意：把原来启动的 8888 关掉，不然端口冲突)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run build</span><br></pre></td></tr></table></figure>

<h3 id="禁止打包的时候自动启动服务"><a href="#禁止打包的时候自动启动服务" class="headerlink" title="禁止打包的时候自动启动服务"></a>禁止打包的时候自动启动服务</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">plugins:[</span><br><span class="line">      <span class="keyword">new</span> BundleAnalyzerPlugin(&#123;</span><br><span class="line">            openAnalyzer: <span class="literal">false</span>, <span class="comment">//是否打开8888服务</span></span><br><span class="line">            analyzerMode: <span class="string">"static"</span>, <span class="comment">// 生成静态的html文件</span></span><br><span class="line">      &#125;),</span><br><span class="line">      <span class="keyword">new</span> HtmlWebpackPlugin(....</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<p>这样打包后会生成一个 report.html 文件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run build</span><br></pre></td></tr></table></figure>

<p>可以在 build 的 build 目录启个服务，访问 report.html 查看结果。使用 serve</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cd</span> build/</span><br><span class="line">serve</span><br></pre></td></tr></table></figure>

<p>访问<a href="http://localhost:5000/report.html可以看到相通的结果。" target="_blank" rel="noopener">http://localhost:5000/report.html可以看到相通的结果。</a></p>
<p>可以控制是否生成分析文件，不然放到生产环境就不好了</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">plugins:[</span><br><span class="line">     proces.env.GENERATE_BUNDLE_ANALYZER===<span class="string">"true"</span>&amp;&amp;<span class="keyword">new</span> BundleAnalyzerPlugin(&#123;</span><br><span class="line">            openAnalyzer: <span class="literal">false</span>, <span class="comment">//是否打开8888服务</span></span><br><span class="line">            analyzerMode: <span class="string">"static"</span>, <span class="comment">// 生成静态的html文件</span></span><br><span class="line">      &#125;),</span><br><span class="line">      <span class="keyword">new</span> HtmlWebpackPlugin(....</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/01/09/%E9%A1%B9%E7%9B%AE%E6%80%A7%E8%83%BD%E5%92%8C%E8%A7%84%E8%8C%83%E6%80%BB%E7%BB%93/">http://blog.wangpengpeng.site/2020/01/09/%E9%A1%B9%E7%9B%AE%E6%80%A7%E8%83%BD%E5%92%8C%E8%A7%84%E8%8C%83%E6%80%BB%E7%BB%93/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%80%A7%E8%83%BD/">性能</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/01/09/%E9%82%A3%E4%BA%9Bnpm%E5%90%AF%E5%8A%A8%E7%94%A8%E5%88%B0%E7%9A%84%E6%8F%92%E4%BB%B6/"><i class="fa fa-chevron-left">  </i><span>那些npm启动用到的插件</span></a></div><div class="next-post pull-right"><a href="/2020/01/08/Electron%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/"><span>Electron入门笔记</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>